package com.example.shushiyunsst.scan

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import com.example.library_common.album.Album
import com.example.library_common.base.BaseFragmentEx
import com.example.library_common.base.LazyFragment
import com.example.shushiyunsst.R
import com.google.zxing.Result
import kotlinx.coroutines.launch
import me.dm7.barcodescanner.zxing.ZXingScannerView

class ScanFragment : BaseFragmentEx(), ZXingScannerView.ResultHandler {

    companion object{
        fun newInstance(): ScanFragment {
            val args = Bundle()
            val fragment = ScanFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private var mScanView: JssScanView? = null

    private var mScanResListener: ScanResListener? = null

    private var isEnterAnimationEnd=false

    fun setScanResListener(mScanResListener: ScanResListener?) {
        this.mScanResListener = mScanResListener
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport = false
    }



    private fun getPicturePath() {
        Album.getInstance()
            .singleImage()
            .onFailed { _, msg ->
                Toast.makeText(_mActivity, msg, Toast.LENGTH_LONG).show()
            }
            .onSuccess {
                if (it.isEmpty()) {
                    return@onSuccess
                }
                val albumFile = it[0]
                try {
                    parserImg(albumFile.path)
                }catch (e:Exception){
                    e.printStackTrace()
                    Toast.makeText(
                        _mActivity,
                        _mActivity.getString(R.string.invaile_q_coding),
                        Toast.LENGTH_LONG
                    ).show()
                }

            }.execute(childFragmentManager)
    }

    private fun parserImg(picturePath: String?) {
        if (TextUtils.isEmpty(picturePath)) {
            Toast.makeText(
                _mActivity,
                _mActivity.getString(R.string.invaile_q_coding),
                Toast.LENGTH_LONG
            ).show()

            return
        }

        val str: String = QRCodeDecoder.syncDecodeQRCode("$picturePath")
        if (str.isNullOrEmpty()) {
            Toast.makeText(
                _mActivity,
                _mActivity.getString(R.string.invaile_q_coding),
                Toast.LENGTH_LONG
            ).show()
        } else {
            mScanResListener?.onResStr(str)

            onBackPressedSupport()
        }

//        lifecycleScope.launch {
//            if (TextUtils.isEmpty(picturePath)) {
//                return@launch
//            }
//            val str: String = QRCodeDecoder.syncDecodeQRCode(picturePath)
//            if (str.isNullOrEmpty()) {
//                Toast.makeText(
//                    _mActivity,
//                    _mActivity.getString(R.string.invaile_q_coding),
//                    Toast.LENGTH_LONG
//                ).show()
//            } else {
//                mScanResListener?.onResStr(str)
//
//                onBackPressedSupport()
//            }
//        }

    }

    override fun onLazyInitView(savedInstanceState: Bundle?) {
        super.onLazyInitView(savedInstanceState)
        mScanView = view?.findViewById(R.id.mJssScanView)

        view?.findViewById<View>(R.id.right_title_text_view)
            ?.setOnClickListener {
                getPicturePath()
            }

        view?.findViewById<View>(R.id.finish)
            ?.setOnClickListener {
                onBackPressedSupport()
            }

        mScanView?.setAutoFocus(true)
        mScanView?.setResultHandler(this)

    }

    override fun onEnterAnimationEnd(savedInstanceState: Bundle?) {
        super.onEnterAnimationEnd(savedInstanceState)

        mScanView?.startCamera()
    }

    override fun onResume() {
        super.onResume()
        if (!isEnterAnimationEnd){
            isEnterAnimationEnd=true
            return
        }
        mScanView?.startCamera()
    }

    override fun onPause() {
        super.onPause()
        mScanView?.stopCameraPreview()
        mScanView?.stopCamera()
    }

    override fun getLayoutId() = R.layout.scan_fragment

    override fun handleResult(rawResult: Result?) {
        val text = rawResult?.text ?: ""
        if (text.isEmpty()) {
            Toast.makeText(
                _mActivity,
                _mActivity.getString(R.string.invaile_q_coding),
                Toast.LENGTH_LONG
            ).show()
        } else {
            mScanResListener?.onResStr(text)

            onBackPressedSupport()
        }

    }
}